import psutil
import os
import signal

def stop_server(port=8001):
    for proc in psutil.process_iter(['pid', 'name', 'connections']):
        try:
            for conn in proc.connections():
                if conn.laddr.port == port:
                    os.kill(proc.pid, signal.SIGTERM)
                    print(f"服务已停止 (PID: {proc.pid})")
                    return
        except (psutil.NoSuchProcess, psutil.AccessDenied):
            pass
    print("服务未运行")

if __name__ == "__main__":
    stop_server() 